<?php

namespace app\api\controller;

use app\api\model\Member;

class Base
{

    //注册功能
    public function register()
    {
        //传入学号，姓名，密码注册
        $mobile=input('get.mobile');
        $name=input('get.name');
        $password=input('get.password');
        if(empty($no))output_error('学号不能为空');
        if(empty($name))output_error('姓名不能为空');
        if(empty($password))output_error('密码不能为空');
        $data=\app\api\model\Member::where('student_no','=',$no)->find();
        if(!empty($data))output_error("学号已经存在");
        //规则1：学号唯一
        $data=array();
        $data['student_no']=$no;
        $data['student_name']=$name;
        $data['student_password']=md5($password);
        $data['student_token']='-1';
        $data['student_gender']='1';
        $data['add_time']=time();
        Member::insert($data);
        output_data('1');
    }

    //找回密码
    public function findpass()
    {
        //通过学号，姓名重置密码
        $mobile=input('get.mobile');
        $name = input('get.name');
        $password = input('get.password');
        if (empty($mobile) || empty($name) || empty($password)) {
            output_error('请填写完整信息');
        }
        //规则1：密码md5加密
        $condition = array();
        $condition[] = ['member_mobile', '=', $mobile];
        $condition[] = ['member_name', '=', $name];
        $data = Member::where($condition)->find();
        if (empty($data)) output_error('学号与姓名不匹配');
        Member::where('member_id', '=', $data['member_id'])
            ->update(array('member_password' => md5($password)));
        output_data('1');
    }

    //登录功能
    public function login()
    {
        //通过学号，密码登录，返回Token
        $mobile=input('get.mobile');
        $password = input('get.password');
        if (empty($mobile) || empty($password)) {
            output_error('请填写完整信息');
        }
        //规则1：token由学号+当前时间戳md5加密生成
        $condition = array();
        $condition[] = ['member_mobile', '=', $mobile];
        $condition[] = ['member_password', '=', md5($password)];
        $data = Member::where($condition)->find();
        if (empty($data)) output_error('学号与密码不匹配');
        //通过学号，密码登录，返回Token
        $token = md5($mobile . time());
        Member::where('member_id', '=', $data['member_id'])
            ->update(array('member_token' => $token));
        output_data($token);

    }

}
